package com.haojii.jdo.basic;
import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.haojii.jdo.utils.PMF;

@SuppressWarnings("serial")
public class UserEncodedStringKeyTestServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		resp.setContentType("text/plain");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		System.out.println("1.0 Create and persist UserLong with @PrimaryKey: String encodedKey");
		UserEncodedString userEncodedString = new UserEncodedString("name1","name1",UserEncodedString.class.getSimpleName());
		pm.makePersistent(userEncodedString);
		
		System.out.println("1.1 Query UserEncodedString with getObjectById, using KeyFactory.stringToKey(encodedKey)");
	    UserEncodedString tmpUserEncodedString = null;
	    String encodedKey = userEncodedString.getEncodedKey();
	    System.out.println(userEncodedString);
	    Key key = KeyFactory.stringToKey(encodedKey);
	    System.out.println("KeyFactory.stringToKey(encodedKey)="+key);
	    tmpUserEncodedString = pm.getObjectById(UserEncodedString.class, key);
	    System.out.println("UserEncodedString==tmpUserEncodedString?"+(userEncodedString==tmpUserEncodedString));
	    
	    System.out.println("1.1(★) Query UserEncodedString with getObjectById, using KeyFactory.createKey(UserEncodedString.class.getSimpleName(), userEncodedString.getKeyName())");
	    key = KeyFactory.createKey(UserEncodedString.class.getSimpleName(), userEncodedString.getKeyName());
	    tmpUserEncodedString = pm.getObjectById(UserEncodedString.class, KeyFactory.keyToString(key));
		System.out.println(" (★)UserEncodedString==tmpUserEncodedString?"+(userEncodedString==tmpUserEncodedString));
		
	    System.out.println("1.1(2) Query UserEncodedString with getObjectById, using userEncodedString.getEncodedKey():"+userEncodedString.getEncodedKey());
	    tmpUserEncodedString = pm.getObjectById(UserEncodedString.class, userEncodedString.getEncodedKey());
	    System.out.println(" (2)UserEncodedString==tmpUserEncodedString?"+(userEncodedString==tmpUserEncodedString));
	    

	    System.out.println("1.2 Query UserEncodedString with name1");
	    tmpUserEncodedString = null;
	    Query query = pm.newQuery(UserEncodedString.class); 
	    query.setFilter("name == nameParam"); 
	    query.setOrdering("date desc"); 
	    query.declareParameters("String nameParam");
	    try { 
	        List<UserEncodedString> results = (List<UserEncodedString>) query.execute("name1"); 
	        System.out.println("All UserEncodedString size:"+results.size());
	        if (results.iterator().hasNext()) { 
	            for (UserEncodedString e : results) { 
	            	tmpUserEncodedString = e;
	                System.out.println(" "+e);
	            } 
	        } else { 
	        	System.out.println("1.2 Query get empty result");
	        } 
	    } finally { 
	        query.closeAll(); 
	    }
	    
	    if (tmpUserEncodedString != null)
	    {
	    	System.out.println("1.3 Update the searched result[0] UserEncodedString with name1->NAME!");
	    	System.out.println("UserEncodedString==tmpUserEncodedString?"+(userEncodedString==tmpUserEncodedString));
	    	tmpUserEncodedString.setName("NAME!");
	    	pm.makePersistent(tmpUserEncodedString);
	    }
	    
	    System.out.println("1.4 Delete UserEncodedString with name1");
	    System.out.println(" ->"+userEncodedString);
	    pm.deletePersistent(userEncodedString);

	    
	    query = pm.newQuery(UserEncodedString.class); 
	    System.out.println("All UserEncodedString size:"+((List<UserEncodedString>) query.execute()).size());
		resp.getWriter().println("Hello, world");
	}
}
